
export function parseTime(time, cFormat) {
  if (arguments.length === 0 || !time) {
    return null
  }
  const format = cFormat || 'y-m-d'
  let date
  if (typeof time === 'object') {
    date = time
  } else if (time.length === 8) {
    date = new Date(time.substring(0, 4) + '/' + time.substring(4, 6) + '/' + time.substring(6, 8))
  } else {
    date = new Date(time)
  }
  console.log(date);
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/(y|m|d|h|i|s)/g, (result, key) => {
    let value = formatObj[key]
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}

export function getRule(data, parentId) {
  const rules = []
  data.forEach(item => {
    rules.push(item.pathUrl)
    rules.push(...getRule(item.children, parentId))
  })
  return rules
}

export function arrayToTree(data) {
  const result = [];
  const map = {};

  data.forEach(item => {
    map[item.id] = { ...item, children: [] };
  });

  data.forEach(item => {
    if (item.parentId && map[item.parentId]) {
      map[item.parentId].children.push(map[item.id]);
    } else {
      result.push(map[item.id]);
    }
  });

  return result;
}